package com.wx.forum.controller;

import com.wx.forum.common.AppResult;
import com.wx.forum.common.ResultCode;
import com.wx.forum.contant.Contants;
import com.wx.forum.model.Article;
import com.wx.forum.model.ArticleReply;
import com.wx.forum.model.User;
import com.wx.forum.service.IArticleReplyService;
import com.wx.forum.service.IArticleService;
import com.wx.forum.service.IUserService;
import com.wx.forum.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "帖子回复接口")
@Slf4j
@RestController
@RequestMapping("/articleReply")
public class ArticleReplyController {
    @Resource
    private IUserService userService;
    @Resource
    private IArticleService articleService;
    @Resource
    private IArticleReplyService articleReplyService;

    @ApiOperation("回复帖子")
    @PostMapping("/create")
    public AppResult create(HttpServletRequest request,
                            @ApiParam("回复id") @RequestParam("articleId") @NonNull Long articleId,
                            @ApiParam("回复内容") @RequestParam("content") @NonNull String content){
        //1.校验用户是否被禁言
        //通过token获取用户信息
        String token = request.getHeader(Contants.USER_TOKEN_HANDER);
        Long userId = JwtUtil.getIdByToken(token);
        User user = userService.selectById(userId);
        //校验
        if (user.getState() == 1) {
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //2.校验帖子是否合法
        Article article = articleService.selectDetailById(articleId);
        if(article == null || article.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        if(article.getState() == 1){
            log.warn(ResultCode.FAILED_FORBIDDEN.toString());
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        //3.构建回复对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setContent(content);
        articleReply.setPostUserId(user.getId());
        //4.调用service
        articleReplyService.creat(articleReply);

        return AppResult.success();
    }
    @ApiOperation("查询回复")
    @GetMapping("/getArticleReply")
    public AppResult<List<ArticleReply>> selectByArticleId(
            @ApiParam("帖子articleId") @RequestParam("articleId") @NonNull Long articleId){
        //1.校验帖子是否合法
        Article article = articleService.selectDetailById(articleId);
        if(article == null || article.getDeleteState() == 1){
            log.warn(ResultCode.FAILED_NOT_EXISTS.toString());
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        if(article.getState() == 1){
            log.warn(ResultCode.FAILED_FORBIDDEN.toString());
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }

        List replyList = articleReplyService.selectByArticleId(articleId);
        if(replyList == null){
            replyList = new ArrayList();
        }
        return AppResult.success(replyList);
    }
}
